package com.javafactroy.kunlong.jsengine;


import com.javafactroy.kunlong.jsengine.base.JsContext;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 设计器之核心功能：
 * 翻译表达式
 * 测试表达式
 * 执行表达式
 * 执行方案
 * */
public class BonusSystem {
    private static ThreadLocal<JsContext> threadLocal = new ThreadLocal<>();

    public static String test() {
        System.out.println("Test By Java2021!");
        return "Test By Java2021";
    }

    public static class JsParam {
        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        String remark;

    }

    public static class JsResult {

        String remark;
        double amount;

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount = amount;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }


    }




    static void  exeJavaScriptExpr() throws ScriptException {
        System.out.println("*******exeJavaScriptExpr");
        System.out.println("\n");

        Map<String, Object> params = new HashMap<>();
        params.put("va", 100);
        params.put("vb", 100);
        String expr = "(va * 10 + vb)/10";
        List<String> paramNames = JavascriptEngine.findExprParamNames(expr);
        Object result = JavascriptEngine.doExpr(expr, params);

        System.out.println("*** 执行表达式 expr[" + expr + "] result = " + result);
    }


    public static void exeJavaScriptCal() throws ScriptException, IOException, NoSuchMethodException {
        System.out.println("*******exeJavaScriptCal");
        ScriptEngine scriptEngine = JavascriptEngine.getScriptEngine();//scriptManager.getEngineByName("nashorn");        // scriptEngine.eval(code);
        JavascriptEngine.loadJsFile(scriptEngine, "fun.js");
        JsResult result = new JsResult();

        Invocable engine = (Invocable) scriptEngine;
        JsParam param = new JsParam();
        param.remark = "input myRemark! ";
        engine.invokeFunction("calFee", param, param, result);
        System.out.println(result.getAmount());
        System.out.println(result.getRemark());
    }

    public static void exeJavaScriptUpper() throws ScriptException, IOException, NoSuchMethodException {
        System.out.println("*******exeJavaScriptUpper");
        ScriptEngine scriptEngine = JavascriptEngine.getScriptEngine();//scriptManager.getEngineByName("nashorn");        // scriptEngine.eval(code);
        JavascriptEngine.loadJsFile(scriptEngine, "fun.js");


        Invocable engine = (Invocable) scriptEngine;
        Object result = engine.invokeFunction("toUpperCase", "leijm");
        System.out.println(result);
    }

    public static void main(String[] args) throws Exception {
        exeJavaScriptCal();
        exeJavaScriptExpr();
        exeJavaScriptUpper();
    }


}


